<?php
/**
 * User: Niuxitong
 * Date: 2021/1/6 20:01
 */
namespace app\admin\controller;

use app\admin\command\Api;
use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Request;
use app\common\controller\Api as UserApi;
use think\Session;

/**
 * 加入会议系列
 * Class Conference
 * @package app\admin\controller
 */
class Conference extends Backend
{
    /**
     * Conference模型对象
     * @var \app\admin\model\Conference
     */
    protected $model = null;
    protected $noNeedLogin = ['config', 'verify', 'joinmeeting'];
    protected $noNeedRight = ["*"];
    protected $searchFields = ["conf.serial"];

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\Conference;   //活动
        $this->model = new \app\admin\model\Conference;

    }

    /**
     * 加入活动
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function joinmeeting(){
        $request = Request::instance();
        $domain = $request->domain();
        // 判断是否是需要强制使用https访问
        if (Config::get('https_jump')) {
            if (substr($domain, 0, 5) != 'https') {
                $domain = 'https'.substr($domain, 4);
            }
        }
        $this->view->assign('weburl', $domain);

        /*
         * 如果从输入会议号加入会议进来的*/
        if ($this->request->isPost()){
            $meeting_id = $this->request->request('meeting_id');
            $type = $this->request->request('type');
            $client = $this->request->request('client');

            // 验证
            if (!$meeting_id) {
                $this->error('请输入有效的活动ID');
            }
            $mtinfo = Db::name('activity')->where(['serial'=>$meeting_id])->field('id')->find();
            if (empty($mtinfo)) {
                $this->error('活动ID不存在');
            }

            if ($type == 'go') {
                $nickname = str_replace(' ', '', $this->request->request('nickname'));
                if (!$nickname) {
                    $this->error('请输入昵称');
                }
                if (strlen($nickname) > 20) {
                    $this->error('昵称长度不能大于20');
                }
                // 获取服务器加密地址
                $host = $this->request->host(); //获取当前不带https的域名my.local-onzoom.cn
                //$scheme =  Api::get_scheme();
                $scheme = $this->request->scheme();   // 获取 https
                $encrypted_extranet = $this->model->getEncryptedExtranet($meeting_id, $host, $scheme);

                $this->success('Success', $encrypted_extranet.$meeting_id.'?displayName='.$nickname.'&weburl='.$domain);
            }
            if ($client == 'jw') {
                $this->success('Success', 'admin/conference/joinmeeting?m='.$meeting_id.'&t=go');
            }else{
                $this->success('Success', 'admin/conference/joinmeeting?m='.$meeting_id);
            }
        }

        // 获取活动号（唯一识别码）
        $meeting_id = $cid = str_replace(' ', '', $this->request->request('m'));

        $type = $this->request->request('t');       //类型
        $client = $this->request->request('c');     //入会方式
        $meeting_cid = $this->request->request('cid');      //通过 输入的会议号入会的方式

        // 接收已登录后的加入会议的参数
        $peerName = $this->request->request('peerName');            //用户唯一标识id 843212379  用户唯一标识Id  （admin表中的serial唯一标识 （不是account表中的serial））
        $displayName = $this->request->request('displayName');      //用户名 牛呵呵
        $PHPSESSID = $this->request->request('PHPSESSID');          //当前用户的 session_id(); 实际值为lkh9ona91ejca4uftn01q5bqt6
        $token = $this->request->request('token');                  //user_token表中的tokenbu

        if ($peerName && $displayName && $PHPSESSID && $token && !$meeting_id) {
            $this->error('活动ID不存在','admin/conference/joinmeeting');
        }
        if ($meeting_id) {
            //$mtinfo = Db::name('conference')->where(['serial'=>$meeting_id])->field('id')->find();
            $mtinfo = Db::name('activity')->where(['serial'=>$meeting_id])->field('id')->find();
            if (empty($mtinfo)) {
                $this->error('活动ID不存在', 'admin/conference/joinmeeting');
            }
        }

        // 获取服务器加密地址
        $host = $this->request->host();       //获取当前不带https的域名my.local-onzoom.cn
        $scheme = $this->request->scheme();   // 获取 https
        $encryptedExtranet = $this->model->getEncryptedExtranet($cid, $host, $scheme);      //获取此会议分配的媒体服务器地址
        $this->view->assign('encrypted_extranet', $encryptedExtranet);

        $this->view->assign('meeting_id', $meeting_id);
        $this->view->assign('peerName', $peerName);
        $userInfo = Session::get('admin');
        if (!empty($userInfo) && !$displayName) {
            $displayName = $userInfo['nickname'];
        }
        $this->view->assign('displayName', $displayName);
        $this->view->assign('PHPSESSID', $PHPSESSID);
        $this->view->assign('token', $token);

        $this->view->assign('displayName', $displayName);
        $this->view->assign('PHPSESSID', $PHPSESSID);
        $this->view->assign('token', $token);

        $showMeetingIdForm = ($client == 'jw') ? 1 : 0;
        $this->view->assign('client', $client);
        $this->view->assign('showMeetingIdForm', $showMeetingIdForm);
        $this->view->assign('meeting_cid', $meeting_cid);

        $join = $this->request->request('join');
        $this->view->assign('join', $join);

        $this->view->assign('title', '加入活动');
        $this->view->engine->layout(false); // 临时关闭当前模板的布局功能，将无法使用框架的js

        if ($type == 'go') {  //
            if ($this->auth->isLogin()) {
                $this->view->assign('displayName', $this->auth->nickname);
            }
            return $this->view->fetch('join_go');
        }else{
            return $this->view->fetch();
        }

    }

    /**
     * 加入会议(演示)
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function joinmeeting888(){    //1977
        $request = Request::instance();
        $domain = $request->domain();
        // 判断是否是需要强制使用https访问
        if (Config::get('https_jump')) {
            if (substr($domain, 0, 5) != 'https') {
                $domain = 'https'.substr($domain, 4);
            }
        }
        $this->view->assign('weburl', $domain);

        // 如果从输入会议号加入会议进来的
        if ($this->request->isPost()){
            $meeting_id = $this->request->request('meeting_id');
            $type = $this->request->request('type');
            $client = $this->request->request('client');
            // 验证
            if (!$meeting_id) {
                $this->error('请输入有效的会议ID');
            }
            $mtinfo = Db::name('conference')->where(['serial'=>$meeting_id])->field('id')->find();
            if (empty($mtinfo)) {
                $this->error('会议ID不存在');
            }

            if ($type == 'go') {
                $nickname = str_replace(' ', '', $this->request->request('nickname'));
                if (!$nickname) {
                    $this->error(__('Please enter your nickname'));
                }
                if (strlen($nickname) > 20) {
                    $this->error(__('Nickname must be within 20 characters'));
                }

                // 获取服务器加密地址
                $host = $this->request->host(); //获取当前不带https的域名my.local-onzoom.cn
                //$scheme =  Api::get_scheme();
                $scheme = $this->request->scheme();   // 获取 https
                $encrypted_extranet = $this->model->getEncryptedExtranet($meeting_id, $host, $scheme);

                $this->success('Success', $encrypted_extranet.$meeting_id.'?displayName='.$nickname.'&weburl='.$domain);
            }
            if ($client == 'jw') {
                $this->success('Success', 'admin/conference/joinmeeting?m='.$meeting_id.'&t=go');
            }else{
                $this->success('Success', 'admin/conference/joinmeeting?m='.$meeting_id);
            }
        }

        $meeting_id = $cid = str_replace(' ', '', $this->request->request('m'));

        $type = $this->request->request('t');       //类型
        $client = $this->request->request('c');     //入会方式
        $meeting_cid = $this->request->request('cid');      //通过 输入的会议号入会的方式

        // 接收已登录后的加入会议的参数
        $peerName = $this->request->request('peerName');            //用户唯一标识id 843212379  用户唯一标识Id  （admin表中的serial唯一标识 （不是account表中的serial））
        $displayName = $this->request->request('displayName');      //用户名 牛呵呵
        $PHPSESSID = $this->request->request('PHPSESSID');          //当前用户的 session_id(); 实际值为lkh9ona91ejca4uftn01q5bqt6
        $token = $this->request->request('token');                  //user_token表中的tokenbu

        if ($peerName && $displayName && $PHPSESSID && $token && !$meeting_id) {
            $this->error('活动ID不存在','admin/conference/joinmeeting');
        }
        if ($meeting_id) {
            $mtinfo = Db::name('conference')->where(['serial'=>$meeting_id])->field('id')->find();
            if (empty($mtinfo)) {
                $this->error('活动ID不存在', 'admin/conference/joinmeeting');
            }
        }

        // 获取服务器加密地址
        $host = $this->request->host();       //获取当前不带https的域名my.local-onzoom.cn
        $scheme = $this->request->scheme();   // 获取 https
        $encryptedExtranet = $this->model->getEncryptedExtranet($cid, $host, $scheme);      //获取此会议分配的媒体服务器地址
        $this->view->assign('encrypted_extranet', $encryptedExtranet);

        $this->view->assign('meeting_id', $meeting_id);
        $this->view->assign('peerName', $peerName);
        $userInfo = Session::get('admin');
        if (!empty($userInfo) && !$displayName) {
            $displayName = $userInfo['nickname'];
        }
        $this->view->assign('displayName', $displayName);
        $this->view->assign('PHPSESSID', $PHPSESSID);
        $this->view->assign('token', $token);

        $this->view->assign('displayName', $displayName);
        $this->view->assign('PHPSESSID', $PHPSESSID);
        $this->view->assign('token', $token);

        $showMeetingIdForm = ($client == 'jw') ? 1 : 0;
        $this->view->assign('client', $client);
        $this->view->assign('showMeetingIdForm', $showMeetingIdForm);
        $this->view->assign('meeting_cid', $meeting_cid);

        $join = $this->request->request('join');
        $this->view->assign('join', $join);

        $this->view->assign('title', __('Join meeting')['zh-cn']);
        $this->view->engine->layout(false); // 临时关闭当前模板的布局功能，将无法使用框架的js

        if ($type == 'go') {  //
            if ($this->auth->isLogin()) {
                $this->view->assign('displayName', $this->auth->nickname);
            }
            return $this->view->fetch('join_go');
        }else{

            return $this->view->fetch();
        }

    }



}